package com.henanuniversity.controller;

import com.henanuniversity.po.Cart;
import com.henanuniversity.service.CartService;
import com.henanuniversity.service.impl.CartServiceImpl;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class CartController {

	public Object saveCart(HttpServletRequest request, HttpServletResponse response) throws Exception{
		Cart cart = new Cart();
		cart.setFoodId(Integer.valueOf(request.getParameter("foodId")));
		cart.setBusinessId(Integer.valueOf(request.getParameter("businessId")));
		cart.setUserId(request.getParameter("userId"));
		CartService service = new CartServiceImpl();
		int result = service.saveCart(cart);
		return result;
	}
	
	public Object updateCart(HttpServletRequest request, HttpServletResponse response) throws Exception{
		Cart cart = new Cart();
		cart.setFoodId(Integer.valueOf(request.getParameter("foodId")));
		cart.setBusinessId(Integer.valueOf(request.getParameter("businessId")));
		cart.setUserId(request.getParameter("userId"));
		cart.setQuantity(Integer.valueOf(request.getParameter("quantity")));
		CartService service = new CartServiceImpl();
		int result = service.updateCart(cart);
		return result;
	}
	
	public Object removeCart(HttpServletRequest request, HttpServletResponse response) throws Exception{
		Cart cart = new Cart();
		cart.setFoodId(Integer.valueOf(request.getParameter("foodId")));
		cart.setBusinessId(Integer.valueOf(request.getParameter("businessId")));
		cart.setUserId(request.getParameter("userId"));
		CartService service = new CartServiceImpl();
		int result = service.removeCart(cart);
		return result;
	}
	
	public Object listCart(HttpServletRequest request, HttpServletResponse response) throws Exception{
		Cart cart = new Cart();
		cart.setUserId(request.getParameter("userId"));
		if(request.getParameter("businessId")!=null) {
			cart.setBusinessId(Integer.valueOf(request.getParameter("businessId")));
		}
		CartService service = new CartServiceImpl();
		List<Cart> list = service.listCart(cart);
		return list;
	}
}
